package cn.edu.scau.cmi.ema.dao;

import cn.edu.scau.cmi.ema.base.CmiPagination;
import cn.edu.scau.cmi.ema.domain.Brand;
import cn.edu.scau.cmi.ema.util.CmiSqlUtil;
import org.hibernate.Session;
import org.hibernate.criterion.DetachedCriteria;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import java.util.Arrays;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.Set;

@Repository("BrandDAO")
@Transactional
public class BrandDAOImpl extends AbstractJpaDao<Brand> implements BrandDAO {

	private final static Set<Class<?>> dataTypes = new HashSet<Class<?>>(Arrays.asList(new Class<?>[] { Brand.class }));

	@PersistenceContext()
	private EntityManager entityManager;

	public BrandDAOImpl() {
		super();
	}

	public EntityManager getEntityManager() {
		return entityManager;
	}

	public Set<Class<?>> getTypes() {
		return dataTypes;
	}

	@Transactional
	public Brand findBrandById(Integer id) throws DataAccessException {

		return findBrandById(id, -1, -1);
	}

	@Transactional
	public Brand findBrandById(Integer id, int startResult, int maxRows) throws DataAccessException {
		try {
			Query query = createNamedQuery("findBrandById", startResult, maxRows, id);
			return (Brand) query.getSingleResult();
		} catch (NoResultException nre) {
			return null;
		}
	}

	@Transactional
	public Set<Brand> findBrandByName(String name) throws DataAccessException {

		return findBrandByName(name, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Brand> findBrandByName(String name, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findBrandByName", startResult, maxRows, name);
		return new LinkedHashSet<Brand>(query.getResultList());
	}

	@Transactional
	public Set<Brand> findBrandByNameContaining(String name) throws DataAccessException {

		return findBrandByNameContaining(name, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Brand> findBrandByNameContaining(String name, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findBrandByNameContaining", startResult, maxRows, name);
		return new LinkedHashSet<Brand>(query.getResultList());
	}

	@Transactional
	public Set<Brand> findBrandByDescription(String description) throws DataAccessException {

		return findBrandByDescription(description, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Brand> findBrandByDescription(String description, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findBrandByDescription", startResult, maxRows, description);
		return new LinkedHashSet<Brand>(query.getResultList());
	}

	@Transactional
	public Set<Brand> findBrandByDescriptionContaining(String description) throws DataAccessException {

		return findBrandByDescriptionContaining(description, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Brand> findBrandByDescriptionContaining(String description, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findBrandByDescriptionContaining", startResult, maxRows, description);
		return new LinkedHashSet<Brand>(query.getResultList());
	}

	@Transactional
	public Brand findBrandByPrimaryKey(Integer id) throws DataAccessException {
		return findBrandByPrimaryKey(id, -1, -1);
	}

	@Transactional
	public Brand findBrandByPrimaryKey(Integer id, int startResult, int maxRows) throws DataAccessException {
		try {
			Query query = createNamedQuery("findBrandByPrimaryKey", startResult, maxRows, id);
			return (Brand) query.getSingleResult();
		} catch (NoResultException nre) {
			return null;
		}
	}

	@Transactional
	public Set<Brand> findBrandsByIds(Set<Integer> ids) throws DataAccessException {
		try {
			Query query = createNamedQuery("findBrandsByIds", -1, -1, ids);
			return new HashSet(query.getResultList());
		} catch (NoResultException nre) {
			return null;
		}
	}

	@Transactional
	public Set<Brand> findAllBrands() throws DataAccessException {

		return findAllBrands(-1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Brand> findAllBrands(int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findAllBrands", startResult, maxRows);
		return new LinkedHashSet<Brand>(query.getResultList());
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Brand> findBrandByCompositeProperities(String sql, int startResult, int maxRows) {
		return new LinkedHashSet<Brand>(createQuery(sql, startResult, maxRows).getResultList());
	}

	@Transactional
	public Set<Brand> findBrandsByDetachedCriteria(CmiPagination page) throws DataAccessException {
		DetachedCriteria detachedCriteria = CmiSqlUtil.createDetachedCriteria(page, Brand.class);
		Session session = (Session) entityManager.getDelegate();

		//.unwrap(Session.class);
		Set<Brand> brands = new HashSet(detachedCriteria.getExecutableCriteria(session)
				.setFirstResult(page.getSearchBeginCursor()).setMaxResults(page.getPaginationSize()).list());

		return brands;
	}

	@Transactional
	public Brand findBrandByPhysicalPrimaryKey(String name) throws DataAccessException {
		return findBrandByPhysicalPrimaryKey(name, -1, -1);
	}

	@Transactional
	public Brand findBrandByPhysicalPrimaryKey(String name, int startResult, int maxRows) throws DataAccessException {
		try {
			Query query = createNamedQuery("findBrandByPhyscialPrimaryKey", startResult, maxRows, name);
			return (Brand) query.getSingleResult();
		} catch (NoResultException nre) {
			return null;
		}
	}

	public boolean canBeMerged(Brand entity) {
		return true;
	}
}
